//
//  LoginTextField.swift
//  whale
//
//  Created by topnet on 2022/3/30.
//

import UIKit


class LoginTextField: ICTextField {
    
    var hintText: String? {
        didSet {
            attributedPlaceholder = hintText?.text(font: font ?? .font(15), color: .placeholder)
        }
    }
    
    var isRight = false
    var isRightTextClosure: ((Bool, String)->())?
    
    private var isPwd: Bool = false
    
    private lazy var disposeBag = DisposeBag()
    
    lazy var rightItem: UIButton = {
        let btn = UIButton(type: .custom)
        btn.iconTitle(isPwd ? IconFont.eye_close : IconFont.clear_circle_bg, fontSize: 20.adapter, for: .normal)
        if isPwd { btn.iconTitle(IconFont.eye_open, fontSize: 20.adapter, for: .selected) }
        btn.setTitleColor(UIColor.placeholder, for: .normal)
        btn.addTarget(self, action: #selector(rightItemClick), for: .touchUpInside)
        return btn
    }()
    
    lazy var leftItem: UIButton = {
        let btn = UIButton(type: .custom)
        return btn
    }()
    
    func setupArea(_ str: String) {
        let text = str.text(font: .font(15), color: .title)
        let at = IconFont.triangle_down.iconText(fontSize: 13.adapter, color: .title)
        text.yy_appendString(" ")
        text.append(at)
        leftItem.setAttributedTitle(text, for: .normal)
    }
    
    required convenience init(isPwd: Bool = false) {
        self.init(frame: .zero)
        self.isPwd = isPwd
        layer.cornerRadius = 5.adapter
        backgroundColor = .bg
        
        rightViewWidth = 40.adapter
        rightViewMode = .whileEditing
        rightView = rightItem
        if isPwd {
            isSecureTextEntry = true
            insets = UIEdgeInsets(left: 10.adapter)
        }else {
            keyboardType = .numberPad
            leftViewWidth = 60.adapter
            leftViewMode = .always
            leftView = leftItem
            setupArea("+86")
            delegate = self
        }
        self.rx.text.orEmpty.changed.subscribe { [weak self] str in
            self?.textChange(str)
        }.disposed(by: disposeBag)
    }
    
    @objc private func rightItemClick() {
        if isPwd {
            rightItem.isSelected = !rightItem.isSelected
            isSecureTextEntry = !rightItem.isSelected
        }else {
            text = nil
        }
    }
    
    private func textChange(_ str: String?) {
        
        isRightTextClosure?(isRight, str ?? "")
    }
    
}

extension LoginTextField: UITextFieldDelegate {
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "" {
            if textField.text?.count != range.location + 1 {
                if range.location == 3 || range.location == 8 {
                    return false
                }
            }
            
        }
        if textField.text?.count ?? 0 >= 13 && string.count > 0 {
            return false
        }
        return true
    }
}
